﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace testASIC
{
   
    
    public partial class Display2 : Form
    {
        public int cellNum=256;
      public  byte[] RegVals;
      public int RegsShowFormat = 1;//1 means hex;0 means dec
        //<to make close_button grey
        const uint SC_CLOSE = 0xf060;
        const uint MF_GRAYED = 0x01;
        const uint MF_BYCOMMAND = 0x0;
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem,
           uint uEnable);
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        static extern bool RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);
        //>

        //child click results in parent's response
        public delegate void contactFatherHandler(object sender,testASIC.child2fatherEventArgs e );
        public event contactFatherHandler askFatherRefresh;
        public event contactFatherHandler askFatherHideMe;
        public event contactFatherHandler askFatherAutoRefresh;
        public Display2()
        {
            RegVals = new byte[cellNum];
            InitializeComponent();
            //
            //this.ControlBox = false;
            //this.Controls.Remove()
            //for (int ii = 0; ii < 256; ii++)
            //{
            //    RegVals[ii] = (byte)ii;
            //    if (ii == 100)
            //        MessageBox.Show(RegVals[ii].ToString());

            //}
        }

        private void Display2_Load(object sender, EventArgs e)
        {
            //<to make close_button grey
            IntPtr hSysMenu;
            hSysMenu = GetSystemMenu(this.Handle, false);
            EnableMenuItem(hSysMenu, SC_CLOSE, MF_GRAYED);
            RemoveMenu(hSysMenu, SC_CLOSE, MF_BYCOMMAND);
            //>
            //<datagridview setup content
            SetupLayout();
            SetupDataGridView();
            PopulateDataGridView();
            //>
        }
        //< datagridview content
        // private void RegsDataGridView_CellFormatting(object sender,
        //System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
        //{
        //    if (this.RegsDataGridView.Columns[e.ColumnIndex].Name == "Release Date")
        //    {
        //        if (e != null)
        //        {
        //            if (e.Value != null)
        //            {
        //                try
        //                {
        //                    e.Value = DateTime.Parse(e.Value.ToString())
        //                        .ToLongDateString();
        //                    e.FormattingApplied = true;
        //                }
        //                catch (FormatException)
        //                {
        //                    Console.WriteLine("{0} is not a valid date.", e.Value.ToString());
        //                }
        //            }
        //        }
        //    }
        //}

        private void addNewRowButton_Click(object sender, EventArgs e)
        {
            this.RegsDataGridView.Rows.Add();
        }

        private void deleteRowButton_Click(object sender, EventArgs e)
        {
            if (this.RegsDataGridView.SelectedRows.Count > 0 &&
                this.RegsDataGridView.SelectedRows[0].Index !=
                this.RegsDataGridView.Rows.Count - 1)
            {
                this.RegsDataGridView.Rows.RemoveAt(
                    this.RegsDataGridView.SelectedRows[0].Index);
            }
        }

        private void SetupLayout()
        {
            // this.Size = new Size(600, 500);

            //addNewRowButton.Text = "Add Row";
            //addNewRowButton.Location = new Point(10, 10);
            addNewRowButton.Click += new EventHandler(addNewRowButton_Click);

            //deleteRowButton.Text = "Delete Row";
            //deleteRowButton.Location = new Point(100, 10);
            deleteRowButton.Click += new EventHandler(deleteRowButton_Click);

            // buttonPanel.Controls.Add(addNewRowButton);
            //buttonPanel.Controls.Add(deleteRowButton);
            //buttonPanel.Height = 50;
            //buttonPanel.Dock = DockStyle.Bottom;

            //this.Controls.Add(this.buttonPanel);
        }

        private void SetupDataGridView()
        {
            //set topleft_header's name
            RegsDataGridView.TopLeftHeaderCell.Value = "Regs_addr";

            //some properties of columns
            RegsDataGridView.ColumnCount = 16;
            RegsDataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.Navy;
            RegsDataGridView.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
            RegsDataGridView.ColumnHeadersDefaultCellStyle.Font =
                 new Font(RegsDataGridView.Font, FontStyle.Bold);
            RegsDataGridView.AutoResizeColumns();
            RegsDataGridView.AutoSizeColumnsMode =
                DataGridViewAutoSizeColumnsMode.AllCells;
            RegsDataGridView.ColumnHeadersBorderStyle =
                DataGridViewHeaderBorderStyle.Single;


            //some properties of all cells
            RegsDataGridView.CellBorderStyle = DataGridViewCellBorderStyle.Single;
            RegsDataGridView.GridColor = Color.Black;

            //some properties of rows
            RegsDataGridView.RowHeadersVisible = true;
            RegsDataGridView.AutoSizeRowsMode =
               DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
            RegsDataGridView.RowHeadersBorderStyle =
                DataGridViewHeaderBorderStyle.Single;
            //RegsDataGridView.AutoResizeRowHeadersWidth;
            RegsDataGridView.RowHeadersDefaultCellStyle.BackColor = Color.Navy;
            RegsDataGridView.RowHeadersDefaultCellStyle.ForeColor = Color.White;
            RegsDataGridView.RowHeadersDefaultCellStyle.Font =
                new Font(RegsDataGridView.Font, FontStyle.Bold);
            RegsDataGridView.RowHeadersWidthSizeMode =
                DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders;

            // RegsDataGridView.Rows[0].Name
            //RegsDataGridView.Columns[0

            //  RegsDataGridView.Columns[0].Name = "Regs";
            String ColumnName;
            for (int ii = 0; ii < 16; ii++)
            {
                if (ii < 10)
                    ColumnName = "0x?" + ii.ToString();//Convert.ToString(;
                else
                {
                    char chtemp = Convert.ToChar((ii - 10 + 'A'));
                    ColumnName = "0x?" + chtemp;
                }
                RegsDataGridView.Columns[ii].Name = ColumnName;
            }
            RegsDataGridView.SelectionMode =
                DataGridViewSelectionMode.FullRowSelect;
            RegsDataGridView.MultiSelect = false;
            RegsDataGridView.Dock = DockStyle.Fill;

            //RegsDataGridView.CellFormatting += new
            //    DataGridViewCellFormattingEventHandler(
            //    RegsDataGridView_CellFormatting);
        }

        public void PopulateDataGridView()
        {

            //Byte[] RegVals = new Byte[256];

            //for table display test
            //for (Byte ii = 0; (int)ii < 253; ii++)
            //   RegVals[ii] = ii;
            //test end

            String[] tmpRow = new String[16];
            String Rowname;
            int nowrowscount = 0;
            int IsExisted = 0;
            //RegsDataGridView.Rows.Clear();
           // MessageBox.Show("")
            //MessageBox.Show("pre"+RegsDataGridView.Rows.Count.ToString());
            for (int jj = 0; jj < 16; jj++)
            {
                if (jj < 10)
                    Rowname = "0x" + jj.ToString() + "?";
                else
                {
                    Rowname = "0x" + Convert.ToChar((jj - 10 + 'A')) + "?";
                }
                for (int kk = 0; kk < 16; kk++)
                {
                    if(RegsShowFormat==1)
                        tmpRow[kk] = (RegVals[jj * 16 + kk]).ToString("X2");
                    else if(RegsShowFormat==0)
                        tmpRow[kk] = (RegVals[jj * 16 + kk]).ToString();
                }
                 nowrowscount = RegsDataGridView.Rows.Count;
                 IsExisted = 0;
                 
                for (int tt = 0; (tt < nowrowscount) && (IsExisted == 0); tt++)
                {
                    
                    if (Rowname == (string)RegsDataGridView.Rows[tt].HeaderCell.Value)
                    {

                        RegsDataGridView.Rows[tt].SetValues(tmpRow);
                        IsExisted = 1;
                    }

                }
                if (IsExisted == 0)
                {
                    RegsDataGridView.Rows.Add(tmpRow);
                    RegsDataGridView.Rows[nowrowscount - 1].HeaderCell.Value = Rowname;
                }

               
            }

        }
        //>datagridview content
        

        private void Display2_FormClosing(object sender, FormClosingEventArgs e)
        {
            MessageBox.Show("dis2_closing");
            //e.Cancel = true;
           
        }

        private void RefreshRegsbutton2_Click(object sender, EventArgs e)
        {
            child2fatherEventArgs args = new child2fatherEventArgs();
            askFatherRefresh(this, args);
            //foreach (int jj in RegVals)
            //{
            //    System.Diagnostics.Debug.WriteLine(RegVals[jj].ToString());
            //}
        }

        private void hidewin_Click(object sender, EventArgs e)
        {
            child2fatherEventArgs args = new child2fatherEventArgs();
            askFatherHideMe(this, args);
        }

        private void DECFormat_CheckedChanged(object sender, EventArgs e)
        {
            if (DECFormat.Checked == true)
            {
                RegsShowFormat = 0;
                HEXFormat.Checked = false;
            }
            PopulateDataGridView();
        }

        private void HEXFormat_CheckedChanged(object sender, EventArgs e)
        {
            if (HEXFormat.Checked == true)
            {
                RegsShowFormat = 1;
                DECFormat.Checked = false;
            }
            PopulateDataGridView();
        }

        private void AutoRefreshCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            child2fatherEventArgs args = new child2fatherEventArgs();
            askFatherAutoRefresh(this, args);
        }
       
    }
    public partial class child2fatherEventArgs : EventArgs
    {

    }
}
